Thread: MyShell [line-editing]

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    MyShell [line-editing]

    Hello guys,

    Im almost finished with writing my own shell, but I have just one thing to finish off. Currently to get the line that the user types, I do a read(). But I want the user to be able to move the cursor to any letter, erase that letter, change that letter to anything, move to the end of the line (wtih a ctr-letter), move to the beginning of the line (with a ctr-letter), etc.

    Im programming on FreeBSD. I already have experience with Termcap and I wrote my own getchar function (which doesnt wait for the user to type enter). But I dont know how to do implement all this. How would I insert a letter? Currently if I erase a letter, it will leave a blank space in the place. Should I store everything in a char * and with each movement/action work with the string?

    Im really just looking for advice. No code is necessary

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are "escape sequences" for insert/delete char, which deletes and inserts and moves the text after the current position.

    The escape sequence should be available in the Termcap data.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Aha I see. Yea I found some stuff on inserting/deleting characters with Termcap. I was wondering how would I keep track of the line that was typed? So that I can parse it afterwards.

    edit: I also found that there are ANSI escape codes, could I use those and not really use the termcap cursor commands? That way I use Termcap only for showing the characters while the user types them and thats it. Although I might be mistaken :/
    Last edited by +Azazel+; 03-25-2008 at 05:53 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, the ANSI codes only work on an ANSI compatible console, which may not be the case for all console variants. That's why there is a termcap functionality to support multiple hardware variants.

    I have written code to read commands, and I just kept the input as a string - then split it when the user hits enter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    yea, well you see the thing is:

    for it to appear as the user types it, after each my_getchar I do a putchar of that character. Afterwards the user modifies the line, does some inserts or something. Now he presses enter.... How do I get this line?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    That's a very boring wheel to reinvent. Look at the readline library. It allows editing, has configurable key bindings, keeps a history, and support tab-completion.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    I know I know, but I still want to do it my way. I mean in the end its "my" project so I might as well redo everything. (+ you learn more, I think )

    But thanks for the suggestion nevertheless

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by +Azazel+ View Post
    I know I know, but I still want to do it my way. I mean in the end its "my" project so I might as well redo everything. (+ you learn more, I think )

    But thanks for the suggestion nevertheless
    Well if you're going to bang on the terminal yourself, at least use termcap instead of hard-coding a bunch of escape sequences. You have to put the terminal in an unbuffered mode, and possibly a raw mode. There's a lot of stuff to do.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Yep, I know that. I have already set the terminal into non-canonical mode + I know how to utilise Termcap. All Im really worried about is getting that line which the user will be modifying.

Popular pages Recent additions subscribe to a feed